﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text.RegularExpressions;

namespace BOMS.WebUI.Web.WebTool
{
    /// <summary>
    /// uploadFileHandler 的摘要说明
    /// </summary>
    public class uploadFileHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            HttpResponse Response = context.Response;
            HttpRequest Request = context.Request;
            //context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");
            Response.CacheControl = "no-cache";




            string EquipMac = Request.QueryString["EquipMac"];
            if (!Regex.IsMatch(EquipMac, @"^[0-9,A-F]{2}\-[0-9,A-F]{2}\-[0-9,A-F]{2}\-[0-9,A-F]{2}\-[0-9,A-F]{2}\-[0-9,A-F]{2}$"))
            {
                //Response.Write("错误:EquipMac格式不符合要求");
                throw new ApplicationException("错误:EquipMac格式不符合要求");
                //return;
            }
            string pRoot = context.Server.MapPath("~/");
            string vFolder = "/Upload/BatteryData/" + EquipMac+"/";

            string s_rpath = pRoot + vFolder.Replace('/','\\');
            //string updir = s_rpath + "\\" + Datedir;
            if (Request.Files.Count > 0)
            {
                try
                {
                    BLL.M_UploadData bll = new BLL.M_UploadData();
                    Model.M_UploadData model = null;

                    string UserChzName = Request["USERNAME"];
                    for (int j = 0; j < Request.Files.Count; j++)
                    {
                        HttpPostedFile uploadFile = Request.Files[j];
                        //if (uploadFile.ContentLength > 0)
                        //{
                            if (!Directory.Exists(s_rpath))
                            {
                                Directory.CreateDirectory(s_rpath);
                            }
                            string extname = Path.GetExtension(uploadFile.FileName);
                            string filename = Path.GetFileNameWithoutExtension(uploadFile.FileName);
                            string NewFileName= filename+"("+DateTime.Now.ToString("yyMMddHHmmssfff")+")"+extname; 
                            uploadFile.SaveAs(string.Format("{0}{1}", s_rpath, NewFileName));//保存文件

                            model = new Model.M_UploadData();
                            model.EquipMac = EquipMac;
                            model.FileName = Path.GetFileName(uploadFile.FileName);
                            model.Size = uploadFile.ContentLength;
                            model.FilePath = vFolder + NewFileName;
                            model.Status = 10;
                            model.Memo = DateTime.Now.ToString("yyyyMMdd HH:mm ") + UserChzName + " 上传数据文件";
                            //model.Memo = DateTime.Now.ToString("yyyyMMdd HH:mm ")  + " 上传数据文件";

                            bll.Add(model);//插入记录

                        //}
                    }
                }
                catch (Exception ex)
                {
                    Response.Write("Error:" + ex.ToString());
                }
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}